package hub.cathome.service.impl;

import hub.annotation.MayNotUse;
import hub.cathome.entity.ResourceLink;
import hub.cathome.entity.tool.ApplicationProperties;
import hub.cathome.entity.tool.FileEraser;
import hub.cathome.mapper.ResourceLinkMapper;
import hub.cathome.service.ResourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("resourceService")
public class ResourceServiceImpl implements ResourceService {
    @Resource(name="resourceLinkMapper")
    private ResourceLinkMapper resourceLinkMapper;

    @Override
    public int insertResourceLink(ResourceLink resourceLink) {
        return resourceLinkMapper.insertResourceLink(resourceLink);
    }

    @Override
    public int updateResourceLink(ResourceLink resourceLink) {
        return resourceLinkMapper.updateResourceLink(resourceLink);
    }

    @Override
    public int deleteResourceLink(Integer id) {
        ResourceLink resourceLink = resourceLinkMapper.getResourceLinkById(id);
        if(resourceLink!=null){
            FileEraser.erase(resourceLink);
        }
        return resourceLinkMapper.deleteResourceLink(id);
    }


    @Override
    public String getResourceLinkPathById(Integer id) {
        return resourceLinkMapper.getResourceLinkPathById(id);
    }
    @Override
    public ResourceLink getResourceLinkById(Integer id) {
        return resourceLinkMapper.getResourceLinkById(id);
    }

    @Override
    public ResourceLink getResourceLinkByPath(String resourcepath) {
        return resourceLinkMapper.getResourceLinkByPath(resourcepath);
    }

    @MayNotUse
    @Override
    public List<ResourceLink> getRecordResourceLinkByCatId(Integer catId) {
        return resourceLinkMapper.getRecordResourceLinkByCatId(catId);
    }
    @MayNotUse
    @Override
    public List<ResourceLink> getShotBlogResourceLinkByShotBlogId(Integer shotBlogId) {
        return resourceLinkMapper.getShotBlogResourceLinkByShotBlogId(shotBlogId);
    }

    /***
     * 获取服务器用存储资源的地址
     * **/
    @Override
    public String getApplicationPath(){
        return ApplicationProperties.server_static_path;
    }


    @Override
    public int deleteAllResourceLinkByShotBlogId(Integer shotBlogId) {
        List<Integer> resourceLinkIds = resourceLinkMapper.getShotBlogResourceLinkIdByShotBlogId(shotBlogId);
        for (Integer resourceLinkId:resourceLinkIds){
            deleteResourceLink(resourceLinkId);
        }
        return resourceLinkIds.size();
    }

    @Override
    public List<ResourceLink> getResourceLinkByUserId(Integer userId) {
        return resourceLinkMapper.getResourceLinkByUserId(userId);
    }


}
